package com.tongfusoft.document.web.controller.persionCenter;

import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.tongfusoft.da.pagehelper.PageInfo;
import com.tongfusoft.document.entity.UserFollows;
import com.tongfusoft.document.entity.Users;
import com.tongfusoft.document.personalCenter.myFollow.service.IMyFollowService;
import com.tongfusoft.exception.ServiceException;
import com.tongfusoft.spring.web.core.controller.BaseController;
import com.tongfusoft.spring.web.core.controller.annotation.UrlDesc;
import com.tongfusoft.spring.web.core.data.ResultModel;
import com.tongfusoft.utils.JsonUtils;

@RequestMapping("/personalCenter")
@Controller
public class MyFollowController extends BaseController{
	
	@Autowired
	@Qualifier("userFollowService")
	public IMyFollowService userFollowService;
	
	@UrlDesc(value = "我的关注页")
	@RequestMapping("/myFollow")
	public String myCollection(Model model){
		
		model.addAttribute("mainPageKey", "myFollow");
		model.addAttribute("mainPage", "personalCenter/myFollow");
		return "base";
	}
	
	@UrlDesc(value = "我的关注文档查询")
	@RequestMapping("/userFollows/query")
	@ResponseBody
	public PageInfo userFollowsQuery(Model model,UserFollows uf, int pageIndex, int pageSize,String query) {
		PageInfo pageInfo =  new PageInfo();
		try {
			if(StringUtils.isNotEmpty(query)){
				Map queryMap = JsonUtils.getMap4Json(query);
				if(queryMap.get("fastText")!=null){
					uf.setDocTitle(queryMap.get("fastText").toString());
				}
			}
			pageInfo = this.userFollowService.follow((Users)super.getUser(), uf, pageSize, pageIndex);
			
		} catch (Exception e) {

			e.printStackTrace();
		}
		return pageInfo;

	}
	
	@UrlDesc(value = "删除关注")
	@RequestMapping("/userFollows/del")
	@ResponseBody
	public ResultModel userFollowsDel(Model model,UserFollows userFollows) {
		
		ResultModel rm = new ResultModel();
		try {
			this.userFollowService.delUserFollowDoc(userFollows,(Users)super.getUser());
			rm.setState("success");
		}catch(ServiceException se){
			
			rm.setState("error");
			rm.setCode(se.getMessage());
			
		} catch (Exception e) {
			rm.setState("error");
			e.printStackTrace();
		}
		return rm;

	}
}
